<?php


/*

	Copyright © 2011 Mike Bennett <mike@kereru.com>

	This file is part of Tazu-Time.

    Tazu-Time is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Tazu-Time is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tazu-Time.  If not, see <http://www.gnu.org/licenses/>.
    
*/


class Workers extends db {

	function __construct($data_file=FALSE) {
		parent::__construct($data_file);
	}

	
	/* Check database structure */
	function database_check() {
		global $gl;

		if ( ! file_exists( $this->db_path.$this->db_file.$this->db_ext ) ) {

			$db_ver = '0.9';
			
			try {
				
				$db = new PDO($this->dsn);
	
				// status
				$qry = "CREATE TABLE status(
					id INTEGER PRIMARY KEY AUTOINCREMENT,
				    param  TEXT,
				    value TEXT
				)";
								
				$db->exec($qry);
	
	
				// workers.
				$qry = "CREATE TABLE workers(
					id INTEGER PRIMARY KEY AUTOINCREMENT,
				    username TEXT,
				    first_name  TEXT,
				    last_name  TEXT,
				    email  TEXT,
				    phone  TEXT,
				    timesheet_file  TEXT,
				    data_type  TEXT
				)";
				$db->exec($qry);

	
				$qry = sprintf("INSERT INTO status (param,value) VALUES ('db_ver','%s');",$db_ver);
				$db->exec($qry);


				$db = null;
				
			} catch (PDOException $e) {
			   $this->error($e->getMessage());
				return;
			}

		}
		
	}
	
	function getWorkers() {
		$qry = sprintf("SELECT * FROM workers;");
		return( $this->rows($qry,FALSE));
	}
	
	function getField($id,$fld='username') {
		return( $this->col($id,'workers',$fld) );
	}

	function updateField($id,$col,$val) {
		$qry = sprintf("UPDATE workers SET %s='%s' WHERE id='%s';",$col,$val,$id);
		return( $this->execute($qry));		
	}

	function addWorker($username,$first_name,$last_name,$email,$timesheet_file) {
		$qry = sprintf("INSERT INTO workers (username,first_name,last_name,email,timesheet_file) VALUES ('%s','%s','%s','%s','%s');",
						$username,$first_name,$last_name,$email,$timesheet_file);
		return( $this->insert($qry) );
	}

	function delWorker($id) {
		$qry = sprintf("DELETE FROM workers WHERE id='%s'",$id);
		return( $this->execute($qry));		
	}



	/**
	*	Check if the given timesheet file is already being used by someone else
	*	
	*	@param	
	*	@return	
	*	
	*/
	function isUsed($id,$file) {
		$qry = sprintf("SELECT * FROM workers WHERE timesheet_file LIKE '%s' AND id <> %s;",$file,$id);
		$result = $this->rows($qry,FALSE);
		$used = (sizeof($result) > 0 );
		return( $used );
	
	}



}

?>